<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>


<%-- <script type="text/javascript" src="<%=request.getContextPath()%>/js/easyui/jquery.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/easyui/locale/easyui-lang-zh_CN.js"></script>
<link rel="stylesheet" type="text/css"
	href="<%=request.getContextPath()%>/js/easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="<%=request.getCon
textPath()%>/js/easyui/themes/icon.css" />
 --%>

<table id="box2" align= "center"></table>

<div id="tb2" style="padding:5px;">
	<div style="margin-bottom:5px;">
		<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="vote_tool.add();">添加</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="vote_tool.edit();">修改</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="vote_tool.remove();">删除</a>
<!-- 		<a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" style="display:none;" id="save" onclick="obj2.save2();">保存</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-redo" plain="true" style="display:none;" id="redo" onclick="obj2.redo2();">取消编辑</a> -->		
	</div>
	<!-- <div style="padding:0 0 0 7px;color:#333;">
		查询帐号：<input type="text" class="textbox" name="username" style="width:110px">
		创建时间从：<input type="text" name="date_from" class="easyui-datebox" editable="false" style="width:110px">
		到：<input type="text" name="date_to" class="easyui-datebox" editable="false" style="width:110px">
		<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="obj2.search();">查询</a>
	</div> -->
</div>
   
<!-- <form id="vote_add" style="margin:0;padding:5px 0 0 25px;color:#333;">

	<p >投票标题：<input type="text" name="vote_title" class="textbox" style="width:500px;"> <button id="btn_add"  onClick="vote_title_add(this)">增加一题</button></p>
	<p>题&nbsp&nbsp&nbsp目：<input type="text" name="title" class="textbox" style="width:500px;">
	类&nbsp&nbsp&nbsp型：
	 <select class="easyui-combobox" name="vote_type" style="width:100px;" onchange="changetype(this)">
	   <option value="" selected='true' >请选择</option>
       <option value="radio">单选题</option>
       <option value="checkbox">多选题</option>
       <option value="text">填空题</option>
     </select>
	</p>
	<div name="div1" ></div>
	<div name="div2" ></div>
	<div name="div3" ></div>
	<div name="div4" ></div>
	<p>题&nbsp&nbsp&nbsp目：<input type='text' name='title' class='textbox' style='width:500px;'> 类&nbsp&nbsp&nbsp型：<select class='easyui-combobox' name='vote_type' style='width:100px;' onchange='changetype(this)'><option value='' selected='true' >请选择</option><option value='radio'>单选题</option><option value='checkbox'>多选题</option><option value='text'>填空题</option></select><div></div>
	<p>选项1：<input name="checkbox_1" class="textbox"  style="width:200px;"></p>
	<p>选项2：<input name="checkbox_2" class="textbox"  style="width:200px;"></p>
	<p>选项3：<input name="checkbox_3" class="textbox"  style="width:200px;"></p>
	<p>选项4：<input name="checkbox_4" class="textbox"  style="width:200px;"></p>
	
</form>
 -->

   
<!-- <form id="adress_edit" style="margin:0;padding:5px 0 0 25px;color:#333;">
    <input type="hidden" name="id" class="textbox" style="width:200px;">
	<p>题目：<input type="text" name="title" class="textbox" style="width:200px;"></p>
	<p>类型：<input type="textbox" name="adress" class="textbox" style="width:200px;"></p>
	<p>电话号码：<input id="phone" class="textbox" name="phone" style="width:200px;"></p>
	<p>传&nbsp&nbsp&nbsp&nbsp真：<input id="fax" class="textbox" name="fax" style="width:200px;"></p>
	<p>服务电话：<input id="servicephone" class="textbox" name="servicephone" style="width:200px;"></p>
	<p>邮政编码：<input id="zipcode" class="textbox" name="zipcode" style="width:200px;"></p>
</form> -->

<form>
    <div id="vote_add2"></div>

</form>


 <script type="text/javascript">
/*  function changetype(select){
   var div = $(select).parent().next() ;
   var select_val = $(select).val() ;
   var select_next = "<strong>最多可选：<select class='easyui-combobox' name='max_checkedCount' style='width:60px;' ><option value='' selected='true' >请选择</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select></strong>" ;
   var div_html = "<p>选&nbsp&nbsp&nbsp项1：<input name='checkbox_1' class='textbox'  style='width:400px;'></p><p>选&nbsp&nbsp&nbsp项2：<input name='checkbox_2' class='textbox'  style='width:400px;'></p><p>选&nbsp&nbsp&nbsp项3：<input name='checkbox_3' class='textbox'  style='width:400px;'></p><p>选&nbsp&nbsp&nbsp项4：<input name='checkbox_4' class='textbox'  style='width:400px;'> <button id='voteanswer_add'  onClick='voteanswer_add()'>增加选项</button></p>" ;
   var btn_html = ""
  switch(select_val)
   {
   case "radio" :
   $(select).next().remove() ;
   div.html(div_html) ;
	break;
   case "checkbox" :
   $(select).next().remove() ;
   $(select).after(select_next);
   div.html(div_html) ;
	break;
	case "text" :
	$(select).next().remove() ;
	div.html("") 
   default:
   	
   } 
   
 }
 function vote_title_add(btn){
   var btn = $(this) ; 
   var temp = "<p>题&nbsp&nbsp&nbsp目：<input type='text' name='title' class='textbox' style='width:500px;'> 类&nbsp&nbsp&nbsp型：<select class='easyui-combobox' name='vote_type' style='width:100px;' onchange='changetype(this)'><option value='' selected='true' >请选择</option><option value='radio'>单选题</option><option value='checkbox'>多选题</option><option value='text'>填空题</option></select><div name='div1'></div>" ;
   
   $("div[name='div1']").last().after(temp);
  
  var temp2 = "<p>题&nbsp&nbsp&nbsp目：<input type='text' name='title' class='textbox' style='width:500px;'> 类&nbsp&nbsp&nbsp型：<select class='easyui-combobox' name='vote_type' style='width:100px;' onchange='changetype(this)'><option value='' selected='true' >请选择</option><option value='radio'>单选题</option><option value='checkbox'>多选题</option><option value='text'>填空题</option></select><div name='div2'></div>" ;
  
 }
  */
 
 
$(function () {  
  $('#box2').datagrid({
        fit : true,
        border : false,
		//width : 1080,
		//url : 'content.json',
		url : 'vote/votedatagrid',
		title : '投票列表',
		/* onClickCell: onClickCell, */
		iconCls : 'icon-search',
		striped : true,
		nowrap : true,
		rownumbers : true,
		//singleSelect : true,
		fitColumns : true,
		columns : [[
			{
				field : 'id',
				title : '编号',
				sortable : true,
				width : 100,
				checkbox : true,
			},
			{
				field : 'title',
				title : '标题',
				sortable : true,
				width : 150,
				editor : {
					type : 'validatebox',
					options : {
						required : true,
					},
				},
			},
			{
				field : 'result_url',
				title : '投票结果',
				sortable : true,
				width : 200,
				formatter : function(value ,rowData ,rowIndex){
				    return '<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true"  ">查看</a>';
				}
			},
			
		]],
		toolbar : '#tb2',
		pagination : true,
		pageSize : 5,
		pageList : [5, 20, 30],
		pageNumber : 1,
		sortName : 'id',
		sortOrder : 'DESC',
		
		//双击编辑
		onDblClickRow : function (rowIndex, rowData) {
		 	$.ajax({
					url : 'vote/findVote',
					type : 'post',
					data : {
						id : rowData.id,
					},
					beforeSend : function () {
						$.messager.progress({
							text : '正在获取中...',
						});
					},
					success : function (data, response, status) {
						$.messager.progress('close');
						
						if (data) {
							$('#adress_edit').form('load', {
								id : data.id,
								company : data.company,
								phone : data.phone,
								adress : data.adress,
								servicephone : data.servicephone,
								fax : data.fax,
								zipcode : data.zipcode,
								//auth_edit : obj[0].auth,
							}).dialog('open');
							
							
							
						} else {
							/* $.messager.alert('获取失败！', '未知错误导致失败，请重试！', 'warning'); */
							
						}
					}
				});		    
		
			/* if (obj2.editRow != undefined) {
				$('#box2').datagrid('endEdit', obj2.editRow);
			}
		
			if (obj2.editRow == undefined) {
				$('#save,#redo').show();
				$('#box2').datagrid('beginEdit', rowIndex);
				obj2.editRow = rowIndex;
			} */
			
		},
		//单元格单机事件
		onClickCell : function (index, field ,value){
		if(field=='result_url'){
		  var rows = $('#box2').datagrid('getRows');
		  var rowData = rows[index];
		  if ($('#tabs').tabs('exists', rowData.title)) {
			  $('#tabs').tabs('select', rowData.title);
		  } else {
		     $('#tabs').tabs('add', {
	             title : rowData.title,
	             closable : true,
	             href : "/vote/result?id="+value,
	       });
	             }
		}
	},
/* 		onAfterEdit : function (rowIndex, rowData, changes) {
			$('#save,#redo').hide();
			
			var inserted = $('#box2').datagrid('getChanges', 'inserted');
			var updated = $('#box2').datagrid('getChanges', 'updated');
			
			var url = info =  '';
			
			//新增用户
			if (inserted.length > 0) {
				url = 'adress/adress_update';
				info = '新增';
			}
			
			//修改用户
			if (updated.length > 0) {
				url = 'adress/adress_update';
				info = '修改';
			}
			
			$.ajax({
				type : 'POST',
				url : url,
				data : {
					id : rowData["id"],
					company : rowData["company"],
					adress : rowData["adress"],
					phone : rowData["phone"],
					fax : rowData["fax"],
					servicephone : rowData["servicephone"],
					zipcode : rowData["zipcode"]
					},
				beforeSend : function () {
					$('#box2').datagrid('loading');
				},
				success : function (data) {
					if (data == "success") {
						$('#box2').datagrid('loaded');
						$('#box2').datagrid('load');
						$('#box2').datagrid('unselectAll');
						$.messager.show({
							title : '提示',
							msg : '地址' + info + '成功！',
						});
						obj2.editRow = undefined;
					}else{
					    $('#box2').datagrid('loaded');
						$('#box2').datagrid('load');
						$('#box2').datagrid('unselectAll');
						$.messager.show({
							title : '提示',
							msg : '用户名重复，'+ info + '失败',
						});
						obj2.editRow = undefined;
					}
				},
			});
			//console.log(rowData);
		}, */
	});
	
	vote_tool = {
/* 		add : function () {
			$('#vote_add').dialog('open');
			 $('input[name="vote_title"]').focus(); 
		}, */
		add : function () {
			 	 $('#tabs').tabs('add', {
	             title : "新建投票",
	             closable : true,
	             href : "./vote/new_vote",
	       });
		},
		edit : function () {
			var rows = $('#box2').datagrid('getSelections');
			if (rows.length > 1) {
				$.messager.alert('警告操作！', '编辑记录只能选定一条数据！', 'warning');
			} else if (rows.length == 1) {
				$.ajax({
					url : 'adress/findadress',
					type : 'post',
					data : {
						id : rows[0].id,
					},
					beforeSend : function () {
						$.messager.progress({
							text : '正在获取中...',
						});
					},
					success : function (data, response, status) {
						$.messager.progress('close');
						
						if (data) {
							$('#adress_edit').form('load', {
								id : data.id,
								company : data.company,
								phone : data.phone,
								adress : data.adress,
								servicephone : data.servicephone,
								fax : data.fax,
								zipcode : data.zipcode,
								//auth_edit : obj[0].auth,
							}).dialog('open');
							
							
							
						} else {
							$.messager.alert('获取失败！', '未知错误导致失败，请重试！', 'warning');
						}
					}
				});
			} else if (rows.length == 0) {
				$.messager.alert('警告操作！', '编辑记录至少选定一条数据！', 'warning');
			}
		},
		remove : function () {
			var rows = $('#box2').datagrid('getSelections');
			if (rows.length > 0) {
				$.messager.confirm('确定操作', '您正在要删除所选的记录吗？', function (flag) {
					if (flag) {
						var ids = [];
						for (var i = 0; i < rows.length; i ++) {
							ids.push(rows[i].id);
						}
						//console.log(ids.join(','));
						$.ajax({
							type : 'POST',
							url : 'adress/deladress',
							data : {
								ids : ids.join(','),
							},
							beforeSend : function () {
								$('#box2').datagrid('loading');
							},
							success : function (data) {
								if (data) {
									$('#box2').datagrid('loaded');
									$('#box2').datagrid('load');
									$('#box2').datagrid('unselectAll');
									$.messager.show({
										title : '提示',
										msg : data + '个地址被删除成功！',
									});
								}
							},
						});
					}
				});
			} else {
				$.messager.alert('提示', '请选择要删除的记录！', 'info');
			}
		},
	};
	$('#vote_add').window({
		width : 900,
		height : 600,
		iconCls : 'icon-add',
		//fit : true,
		title : '窗口',
		collapsible : false,
		minimizable : false,
		maximizable : false,
		closable : false,
		closed : false,
		zIndex : 9999,
		draggable : false,
		resizable : false,
		shadow : false,
		modal : true,
			
		onClose : function () {
			alert('关闭后触发！');
		}
		
	});
	
	/* $('#vote_add').dialog({
		width : 900,
		title : '新增管理',
		modal : true,
		closed : true,
		buttons : [{
			text : '提交',
			handler : function () {
				if ($('#vote_add').form('validate')) {
				    var adress = $("#vote_add").serialize();
					$.ajax({
						url : 'vote/vote_update',
						type : 'post',
						data : adress,
						beforeSend : function () {
							$.messager.progress({
								text : '正在新增中...',
							});
						},
						success : function (data, response, status) {
							$.messager.progress('close');
							
							if (data == "success") {
								$.messager.show({
									title : '提示',
									msg : '新增成功',
								});
								$('#adress_add').dialog('close').form('reset');
								$('#box2').datagrid('reload');
							} else {
								$.messager.alert('新增失败！', '未知错误导致失败，请重试！', 'warning');
							}
						}
					});
				}
			},
		},{
			text : '取消',
			iconCls : 'icon-redo',
			handler : function () {
				$('#adress_add').dialog('close').form('reset');
			},
		}],
	}); */
	$('#adress_edit').dialog({
		width : 400,
		title : '修改管理',
		modal : true,
		closed : true,
		buttons : [{
			text : '提交',
			handler : function () {
				if ($('#adress_edit').form('validate')) {
				    var adress = $("#adress_edit").serialize();
					$.ajax({
						url : 'adress/adress_update',
						type : 'post',
						data : adress,
						beforeSend : function () {
							$.messager.progress({
								text : '正在提交中...',
							});
						},
						success : function (data, response, status) {
							$.messager.progress('close');
							
							if (data == "success") {
								$.messager.show({
									title : '提示',
									msg : '修改成功',
								});
								$('#adress_edit').dialog('close').form('reset');
								$('#box2').datagrid('reload');
							} else {
								$.messager.alert('修改失败！', '未知错误导致失败，请重试！', 'warning');
							}
						}
					});
				}			
			},
		},{
			text : '取消',
			iconCls : 'icon-redo',
			handler : function () {
				$('#adress_edit').dialog('close').form('reset');
			},
		}],
	});
	
});
  
 /*  function wacthvote(btn){
    $btn =$(btn);
    $td = $btn.parents("td") ;
    title = $($td[])
    url= $($td[filed='result_url']).val();
    alert($td); 
   $('#tabs').tabs('add', {
	title : title,
	closable : true,
	href : "/vote/result?id="+url,
	});
}			
 */
  /*  $(function(){
     $("select[name='vote_type']").each(
        function(){
          alert($(this).val());
          
          if($(this).val()==radio){
             var div = $($(this)+div) ;
             div.html("<a>bbbbb</a>") ;
             }
          
        }
     
     )
    
    } 
 )*/

    
    </script>

